#!/bin/bash

#配置druid
function conf_druid() {
  if [ -d $HYRENTMP_HOME ]; then rm -rf $HYRENTMP_HOME;fi
  mkdir_check $HYRENTMP_HOME
  tar -zxf $XDIR/packages/druid-0.10.1-bin.tar.gz -C $HYRENTMP_HOME
  echo_info "confure files."

  #使用预定义文件common.runtime.properties替换默认的common.runtime.properties
  cp -f $XDIR/conf/druid/common.runtime.properties $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common
  #该文件修改内容有：druid.extensions.loadList=["druid-kafka-eight", "druid-s3-extensions", "druid-histogram", "druid-datasketches", "druid-lookups-cached-global", "mysql-metadata-storage"]
  #该文件修改内容有：druid.storage.storageDirectory=HRS_DRUID_DATADIR/segments
  #该文件修改内容有：druid.indexer.logs.directory=HRS_DRUID_DATADIR/indexing-logs
  sed -i "s%HRS_DRUID_DATADIR%$HRS_DRUID_DATADIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  #该文件修改内容有：注释掉Metadata storage的For Derby server的内容，放开For PostgreSQL的内容
  sed -i "s%HRS_PGSQL_HOST%$HRS_PGSQL_HOST%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  sed -i "s%HRS_PGSQL_DBNAME%$HRS_PGSQL_DBNAME%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  sed -i "s%HRS_PGSQL_DBUSER%$HRS_PGSQL_DBUSER%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  sed -i "s%HRS_PGSQL_DBPAWD%$HRS_PGSQL_DBPAWD%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  #修改文件内容有：druid.zk.service.host=zk.host.ip
  local value_zkhosts=($HRS_ZOOKEEPER_HOSTS)
    for i in ${value_zkhosts[@]}; do
      zkhosts=$zkhosts,$i:$HRS_ZOOKEEPER_clientPort 
    done
    zkhosts=${zkhosts:1} 
  sed -i "s%zk.host.ip%$zkhosts%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/_common/common.runtime.properties
  
  #使用broker预定义文件jvm.config替换默认的jvm.config
  cp -f $XDIR/conf/druid/broker/jvm.config $HYRENTMP_HOME/druid-0.10.1/conf/druid/broker
  #该文件修改内容有：-XmsHRS_DRUID_BROKER_MEMORY、-XmxHRS_DRUID_BROKER_MEMORY、-Djava.io.tmpdir=HRS_DRUID_TMPDIR
  sed -i "s%HRS_DRUID_BROKER_MEMORY%$HRS_DRUID_BROKER_MEMORY%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/broker/jvm.config
  sed -i "s%HRS_DRUID_TMPDIR%$HRS_DRUID_TMPDIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/broker/jvm.config
  #使用broker预定义文件runtime.properties替换默认的runtime.properties
  cp -f $XDIR/conf/druid/broker/runtime.properties $HYRENTMP_HOME/druid-0.10.1/conf/druid/broker
  #该文件修改内容有：druid.processing.buffer.sizeBytes=419430400、druid.sql.enable=true、druid.sql.avatica.enable=true
  
  #使用coordinator预定义文件jvm.config替换默认的jvm.config
  cp -f $XDIR/conf/druid/coordinator/jvm.config $HYRENTMP_HOME/druid-0.10.1/conf/druid/coordinator
  #该文件修改内容有：-XmsHRS_DRUID_COORDINATOR_MEMORY、-XmxHRS_DRUID_COORDINATOR_MEMORY、-Djava.io.tmpdir=HRS_DRUID_TMPDIR、-Dderby.stream.error.file=HRS_DRUID_DATADIR/derby.log
  sed -i "s%HRS_DRUID_COORDINATOR_MEMORY%$HRS_DRUID_COORDINATOR_MEMORY%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/coordinator/jvm.config
  sed -i "s%HRS_DRUID_TMPDIR%$HRS_DRUID_TMPDIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/coordinator/jvm.config
  sed -i "s%HRS_DRUID_DATADIR%$HRS_DRUID_DATADIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/coordinator/jvm.config
  
  #使用historical预定义文件jvm.config替换默认的jvm.config
  cp -f $XDIR/conf/druid/historical/jvm.config $HYRENTMP_HOME/druid-0.10.1/conf/druid/historical
  #该文件修改内容有：-XmsHRS_DRUID_HISTORICAL_MEMORY、-XmxHRS_DRUID_HISTORICAL_MEMORY、-Djava.io.tmpdir=HRS_DRUID_TMPDIR
  sed -i "s%HRS_DRUID_HISTORICAL_MEMORY%$HRS_DRUID_HISTORICAL_MEMORY%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/historical/jvm.config
  sed -i "s%HRS_DRUID_TMPDIR%$HRS_DRUID_TMPDIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/historical/jvm.config
  #使用historical预定义文件runtime.properties替换默认的runtime.properties
  cp -f $XDIR/conf/druid/historical/runtime.properties $HYRENTMP_HOME/druid-0.10.1/conf/druid/historical
  #该文件修改内容有：druid.processing.buffer.sizeBytes=436870912、druid.segmentCache.locations=[{"path":"HRS_DRUID_DATADIR/segment-cache","maxSize"\:130000000000}]
  sed -i "s%HRS_DRUID_DATADIR%$HRS_DRUID_DATADIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/historical/runtime.properties
  
  #使用middleManager预定义文件jvm.config替换默认的jvm.config
  cp -f $XDIR/conf/druid/middleManager/jvm.config $HYRENTMP_HOME/druid-0.10.1/conf/druid/middleManager
  #该文件修改内容有：-Djava.io.tmpdir=HRS_DRUID_TMPDIR
  sed -i "s%HRS_DRUID_TMPDIR%$HRS_DRUID_TMPDIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/middleManager/jvm.config
  #使用middleManager预定义文件runtime.properties替换默认的runtime.properties
  cp -f $XDIR/conf/druid/middleManager/runtime.properties $HYRENTMP_HOME/druid-0.10.1/conf/druid/middleManager
  #该文件修改内容有：druid.indexer.task.baseTaskDir=HRS_DRUID_DATADIR/task、druid.indexer.task.hadoopWorkingPath=HRS_DRUID_DATADIR/hadoop-tmp
  sed -i "s%HRS_DRUID_DATADIR%$HRS_DRUID_DATADIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/middleManager/runtime.properties
  
  #使用overlord预定义文件jvm.config替换默认的jvm.config
   cp -f $XDIR/conf/druid/overlord/jvm.config $HYRENTMP_HOME/druid-0.10.1/conf/druid/overlord
   #该文件修改内容有：-XmsHRS_DRUID_OVERLORD_MEMORY、-XmxHRS_DRUID_OVERLORD_MEMORY、-Djava.io.tmpdir=HRS_DRUID_TMPDIR
   sed -i "s%HRS_DRUID_OVERLORD_MEMORY%$HRS_DRUID_OVERLORD_MEMORY%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/overlord/jvm.config
   sed -i "s%HRS_DRUID_TMPDIR%$HRS_DRUID_TMPDIR%g" $HYRENTMP_HOME/druid-0.10.1/conf/druid/overlord/jvm.config

   echo_info "conf_druid success"
}

#安装druid
function setup_druid() {
  echo_start "$1 ... ..."
  confirm_operate "install druid"
  if [ $? -eq 0 ]; then
    if [ "x$HRS_DRUID_HOSTS" = "x" ]; then
      echo_warn "The variable HRS_DRUID_HOSTS does not assign a value, Unable to install! "
      echo_passed "$1 SKIP"
      return 0
    fi
    conf_druid
    local value_Druidhosts=($HRS_DRUID_HOSTS)
    for i in ${value_Druidhosts[@]}; do
      ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_DRUID_HOME/bin ]; then cd $HRS_DRUID_HOME/bin;./broker.sh stop;./historical.sh stop;fi"
      ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_DRUID_HOME/bin ]; then cd $HRS_DRUID_HOME/bin;./coordinator.sh stop;./overlord.sh stop;fi" 
      ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_DRUID_HOME/bin ]; then cd $HRS_DRUID_HOME/bin;./middleManager.sh stop;rm -rf $HRS_DRUID_HOME;fi" 
      ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_DRUID_DATADIR ]; then rm -rf $HRS_DRUID_DATADIR;fi"
      ssh -o StrictHostKeyChecking=no $i "if [ -d $HRS_DRUID_TMPDIR ]; then rm -rf $HRS_DRUID_TMPDIR;fi"
      ssh -o StrictHostKeyChecking=no $i "mkdir -p $HRS_DRUID_HOME" || { die "make druid_dir fail!"; } 
      ssh -o StrictHostKeyChecking=no $i "mkdir -p $HRS_DRUID_DATADIR" || { die "make druid_datadir fail!"; } 
      ssh -o StrictHostKeyChecking=no $i "mkdir -p $HRS_DRUID_TMPDIR" || { die "make druid_tmpdir fail!"; } 
      scp -r $HYRENTMP_HOME/druid-0.10.1/* $i:$HRS_DRUID_HOME > /dev/null
    done
  
    #启动broker
    local value_Druid_brokerhosts=($HRS_DRUID_BROKER_HOSTS)
    for i in ${value_Druid_brokerhosts[@]};do
      ssh -o StrictHostKeyChecking=no $i "cd $HRS_DRUID_HOME/bin && ./broker.sh start" || { die "start broker fail!"; }
    done
  
    #启动historical
    local value_Druid_historicalhosts=($HRS_DRUID_HISTORICAL_HOSTS)
    for i in ${value_Druid_historicalhosts[@]};do
      ssh -o StrictHostKeyChecking=no $i "cd $HRS_DRUID_HOME/bin && ./historical.sh start" || { die "start historical fail!"; }
    done
  
    #启动coordinator
    local value_Druid_coordinatorhosts=($HRS_DRUID_COORDINATOR_HOSTS)
    for i in ${value_Druid_coordinatorhosts[@]};do
      ssh -o StrictHostKeyChecking=no $i "cd $HRS_DRUID_HOME/bin && ./coordinator.sh start" || { die "start coordinator fail!"; }  
    done
  
    #启动overlord
    local value_Druid_overlordhosts=($HRS_DRUID_OVERLORD_HOSTS)
    for i in ${value_Druid_overlordhosts[@]};do
      ssh -o StrictHostKeyChecking=no $i "cd $HRS_DRUID_HOME/bin && ./overlord.sh start" || { die "start coordinator fail!"; } 
    done
  
    #启动middleManager
    local value_Druid_middleManagerhosts=($HRS_DRUID_MIDDLEMANAGER_HOSTS)
    for i in ${value_Druid_middleManagerhosts[@]};do
      ssh -o StrictHostKeyChecking=no $i "cd $HRS_DRUID_HOME/bin && ./middleManager.sh start" || { die "start coordinator fail!"; }
    done
    
    echo_passed "$1 done."
  else
    echo_passed "$1 SKIP"
  fi
}
